/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * callbacks.c
 * Copyright (C) Juan Rafael García Blanco 2009 <jgblanco.a@gmail.com>
 * 
 * callbacks.c is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * callbacks.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>

#include "callbacks.h"
#include "window-frame.h"
#include "file-treeview.h"

extern char direction[];
extern GtkWidget *entry;
extern GtkWidget *file_view;

gboolean
safe_quit( GtkWidget *widget, gpointer data )
{
	//TODO
	gtk_main_quit();
	return TRUE;
}

gboolean
on_entry_enter( GtkWidget *widget, gpointer data )
{
	strcpy( direction, gtk_entry_get_text( GTK_ENTRY( entry ) ) );
	fill_mv_from_direction();
	
	return TRUE;
}

gboolean
on_file_view_row_activated( GtkWidget *widget, gpointer data )
{
	GtkTreeIter it;
	GtkTreeSelection *selection;
	GtkTreeModel *model;
	char *name;
	
	model = gtk_tree_view_get_model( GTK_TREE_VIEW( file_view ) );
	selection  = gtk_tree_view_get_selection( GTK_TREE_VIEW( file_view ) );
	
	if ( gtk_tree_selection_count_selected_rows( selection ) > 1 )
		return FALSE;
	
	gtk_tree_selection_get_selected( selection, &model, &it );
	
	//gtk_tree_model_get_iter( model, &it, path );
	gtk_tree_model_get( model, &it, 0, &name, -1 );
	
	if ( name[strlen( name ) - 1] != '/' )
		return FALSE;
	
	strcat( direction, name );
	gtk_entry_set_text( GTK_ENTRY( entry ), direction );
	fill_mv_from_direction();
	
	free( name );
	
	return TRUE;
}

gboolean
on_up_button_clicked( GtkWidget *widget, gpointer data )
{
	int len;

	len = strlen( direction );
	if ( direction[len - 1] == '/' )
		len -= 2;
	else
		len -= 1;
	
	while ( direction[len] != '/' && len > 0 )
		--len;
	direction[len + 1] = '\0';

	gtk_entry_set_text( GTK_ENTRY( entry ), direction );
	fill_mv_from_direction();
	
	return TRUE;
}